package com.learn.actionplatfo.controller;

import com.learn.actionplatfo.bean.Good;
import com.learn.actionplatfo.bean.GoodForUser;
import com.learn.actionplatfo.bean.Price;
import com.learn.actionplatfo.bean.User;
import com.learn.actionplatfo.service.GoodForUserService;
import com.learn.actionplatfo.service.GoodService;
import com.learn.actionplatfo.service.PriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
public class PriceAndGoodController {

    @Autowired
    GoodService goodService;

    @Autowired
    PriceService priceService;

    @Autowired
    GoodForUserService goodForUserService;

    @RequestMapping("/toAddGoodAndPrice")
    public String test(){
        return "addGoodAndPrice";
    }

    @RequestMapping("/addGoodAndPrice")
    public String addGoodAndPrice(Good good, Price price, Model model){
        price.setPriceNow(price.getPriceStart());
        int max=Math.max(goodService.getAllGoodMaxId(), priceService.getPriceMaxId());
        max++;
        good.setGoodNo(max);
        good.setGoodDdl(new Date());
        price.setPriceGoodNo(max);
        priceService.addPrice(price);
        goodService.addGood(good);
        return "main";
    }

    @RequestMapping("/getGFUByUpPutUserId")
    public String getGFUByUpPutUserId(HttpSession session,Model model){
        User user = (User)session.getAttribute("loginUser");
        int userNo = user.getUserNo();
        List<GoodForUser> gfUbyUpUptUser = goodForUserService.getGFUbyUpUptUser(userNo);

        model.addAttribute("GFUList",gfUbyUpUptUser);
        return "deal";
    }
}
